//package com.yunhe.device.service.base.impl;
//
//import com.yunhe.device.domain.base.EnergyUnitType;
//import com.yunhe.common.exception.ArgumentErrorException;
//import com.yunhe.common.exception.ObjectNotFoundException;
//import com.yunhe.common.exception.UniqueConstraintsException;
//import com.yunhe.device.repository.base.EnergyUnitTypeRepository;
//import com.yunhe.device.service.base.EnergyUnitTypeService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.Optional;
//
///**
// * 能量单元类型服务类实现
// * @desc 能量单元类型服务类实现
// * @author liuronglei
// */
//@Service
//public class EnergyUnitTypeServiceImpl implements EnergyUnitTypeService {
//
//    @Autowired
//    private EnergyUnitTypeRepository energyUnitTypeRepository;
//
//    private void processBeforeSave(EnergyUnitType energyUnitType)
//            throws ArgumentErrorException, UniqueConstraintsException {
//        if (energyUnitType.getCode() == null
//                || energyUnitType.getName() == null
//                || energyUnitType.getTitle() == null
//                || energyUnitType.getActivity() ==null
//                || energyUnitType.getSn() == null) {
//            throw new ArgumentErrorException("参数不能为空");
//        }
//        String name = energyUnitType.getName();
//        EnergyUnitType energyUnitTypeByName = energyUnitTypeRepository.findByName(name);
//        if (energyUnitTypeByName != null && !energyUnitType.getId().equals(energyUnitTypeByName.getId())) {
//            throw new UniqueConstraintsException("已存在名称为" + name + "的能量单元类型");
//        }
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public EnergyUnitType create(EnergyUnitType energyUnitType)
//            throws ArgumentErrorException, UniqueConstraintsException {
//        energyUnitType.setId(null);
//        processBeforeSave(energyUnitType);
//        return energyUnitTypeRepository.save(energyUnitType);
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void deleteById(Long id) {
//        energyUnitTypeRepository.deleteById(id);
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public EnergyUnitType update(EnergyUnitType energyUnitType)
//            throws ArgumentErrorException, UniqueConstraintsException {
//        processBeforeSave(energyUnitType);
//        return energyUnitTypeRepository.save(energyUnitType);
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public EnergyUnitType update(Long id,
//                                 Long code,
//                                 String name,
//                                 String title,
//                                 String sn,
//                                 Boolean activity)
//            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
//        Optional<EnergyUnitType> energyUnitTypeOpt = energyUnitTypeRepository.findById(id);
//        if (!energyUnitTypeOpt.isPresent()) {
//            throw new ObjectNotFoundException("找不到ID为" + id + "的能量单元类型");
//        }
//        EnergyUnitType energyUnitType = energyUnitTypeOpt.get();
//        Optional.ofNullable(code).ifPresent(energyUnitType::setCode);
//        Optional.ofNullable(name).ifPresent(energyUnitType::setName);
//        Optional.ofNullable(title).ifPresent(energyUnitType::setTitle);
//        Optional.ofNullable(sn).ifPresent(energyUnitType::setSn);
//        Optional.ofNullable(activity).ifPresent(energyUnitType::setActivity);
//        processBeforeSave(energyUnitType);
//        return energyUnitTypeRepository.save(energyUnitType);
//    }
//
//    @Override
//    public EnergyUnitType findById(Long id) {
//        return energyUnitTypeRepository.findById(id).orElse(null);
//    }
//
//    @Override
//    public Iterable<EnergyUnitType> search() {
//        return energyUnitTypeRepository.findAll();
//    }
//}
